
/*    This file is part of the Managarm operating system.
 *   Copyright (C) 2007, 2008, 2009  Alexander van der Grinten
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

package org.managarm.jmanavalib.core;

public final class VaMthInfo extends VaObject {
	public static final int FLAG0_STATIC = 1;
	public static final int FLAG0_NATIVE = 2;
	public static final int FLAG0_ABSTRACT = 4;
	public static final int FLAG0_FINAL = 8;
	
	// identifier of the virtual method
	public final String ident;
	
	// class that contains the method declaration
	public final VaClsInfo clsInfo;
	
	// return type of the virtual method
	public final VaDatatype returnType;
	
	// flags of the virtual method
	public final int flags0;
	
	// arguments of the virtual method
	private final VaDatatype[] arguments;
	
	public VaMthInfo(String ident, VaClsInfo cls,
			VaDatatype type, VaDatatype[] arguments, int flags) {
		// save the class and the identifier
		this.ident = ident;
		this.clsInfo = cls;
		
		// save the return type and the arguments
		this.returnType = type;
		this.arguments = arguments.clone();
		
		// save the method flags
		this.flags0 = flags;
	}
	
	// returns true if this method is compatible to "other"
	public final boolean compatible(final VaMthInfo other) {
		if(ident.equals(other.ident) == false)
			return false;
		
		// compare the return type
		if(this.returnType != null) {
			if(this.returnType.compatible(other.returnType) == false)
				return false;
		}else if(other.returnType != null)
			return false;
		
		// compare the arguments
		if(arguments.length != other.arguments.length)
			return false;
		for(int i = 0; i < arguments.length; i++)
			if(!arguments[i].compatible(other.arguments[i]))
				return false;
		
		if((flags0 & FLAG0_STATIC)
				!= (other.flags0 & FLAG0_STATIC))
			return false;
		
		return true;
	}
	
	// builds a hash code based on fields
	// significant for compatibility
	public int compatibilityHash() {
		int hash = 17;
		hash = 37 * hash + ident.hashCode();
		hash = 37 * hash + returnType.compatibilityHash();
		for(int i = 0; i < arguments.length; i++)
			hash = 37 * hash + arguments[i].compatibilityHash();
		hash = 37 * hash + ((flags0 & FLAG0_STATIC) != 0 ? 0 : 1);
		return hash;
	}
	
	// see java.lang.Object
	@Override public boolean equals(Object other) {
		if(other instanceof VaMthInfo == false)
			return false;
		final VaMthInfo mthinfo = (VaMthInfo)other;
		if(compatible(mthinfo) == false)
			return false;
		if(clsInfo.compatible(mthinfo.clsInfo) == false)
			return false;
		return true;
	}

	// FIXME: equals, hashCode and compatible() are broken (clsInfo)
	// see java.lang.Object
	public int hashCode() {
		return compatibilityHash() + clsInfo.compatibilityHash();
	}
	
	// returns the argument count
	public int getArgumentCnt() {
		return arguments.length;
	}
	
	// returns the type of argument "index"
	public VaDatatype getArgument(int index) {
		return arguments[index];
	}
}
